package icu.lookyousmileface.nio.basic;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author shenj
 * @title: GroupClient
 * @projectName NettyPro
 * @date 2020/12/2 0:04
 */
public class GroupClient {

    private final String host = "127.0.0.1";
    private final int port = 7799;
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;
    /**
     * @author shenj
     * @title: GroupClient
     * @date 2020/12/2  14:20
     * 无参构造
     */
    public GroupClient() {

        try {
            selector = Selector.open();
            socketChannel = SocketChannel.open(new InetSocketAddress(host,port));
            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_READ);
            username = socketChannel.getLocalAddress().toString().substring(1);
            System.out.println(username+"is ok!!!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @author shenj
     * @title: sendInfo
     * @date 2020/12/2  14:12
     * 发送信息到服务端
     */
    public  void sendInfo(String info){
        info = username+"说:"+info;
        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @author shenj
     * @title: readInfo
     * @date 2020/12/2  14:11
     * 读取服务器发送的信息
     */
    public void readInfo(){
        try {
            int status = selector.select();
            if (status>0){
                Iterator<SelectionKey> key_iterator = selector.selectedKeys().iterator();
                while (key_iterator.hasNext()) {
                    SelectionKey s = key_iterator.next();
                    if (s.isReadable()) {
                        SocketChannel socketChannel = (SocketChannel) s.channel();
                        ByteBuffer dataBuffer = ByteBuffer.allocate(1024);
                        try {
                            socketChannel.read(dataBuffer);
                            String msg = new String(dataBuffer.array());
                            System.out.println(msg.trim());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                key_iterator.remove();
            }else {
//                System.out.println("没有可用的通道...");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @author shenj
     * @title: main
     * @date 2020/12/2  14:20
     * 主方法
     */
    public static void main(String[] args) {
        GroupClient chatClient = new GroupClient();
        new Thread(){
            @Override
            public void run() {
                while (true){
                    chatClient.readInfo();
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //发送给服务端
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String str = scanner.nextLine();
            chatClient.sendInfo(str);
        }
    }
}
